package triangleWorm;

import processing.core.PApplet;
import processing.core.PVector;

public class SimpleNoiseMovement implements IWormMovement {

	private PApplet p;
	private PVector wSize;
	private PVector wCenter;
	
	//
	private PVector position;
	
	//Noise seeds
	private int xSeed;
	private int ySeed;
	private int zSeed;
	
	//Noise offsets
	private float xOff = 0f;
	private float yOff = 0f;
	private float zOff = 0f;
	
	//Noise iteration scale
	private final float NOISESCALE = 0.02f;
	
	public SimpleNoiseMovement() {
		//Initiate position to (0, 0, 0)
		position = new PVector();
		
		//Get seeds
		xSeed = (int) (Math.random() * (double) Integer.MAX_VALUE);
		ySeed = (int) (Math.random() * (double) Integer.MAX_VALUE);
		zSeed = (int) (Math.random() * (double) Integer.MAX_VALUE);
	}
	
	
	public void init(PApplet processing, PVector worldSize, PVector worldCenter) {
		p = processing;		
		wSize = worldSize;
		wCenter = worldCenter;
		
		//Set position to center of world
		position.set(worldCenter.x, worldCenter.y, worldCenter.z);
	}

	public void update() {
		System.out.println("SIZE: " + wSize.toString());
		System.out.println("CENTER: " + wCenter.toString());
		
		//Get X value
		//Fix noise
		p.noiseSeed(xSeed);
		//Get noise
		float xNoiseValue = p.noise(xOff);
		//Calculate new position in world, world center +/- half world size
		float xPosition = wCenter.x + (wSize.x / 2 -  wSize.x * xNoiseValue);
		
		//Get Y value
		p.noiseSeed(ySeed);
		float yNoiseValue = p.noise(yOff);
		float yPosition = wCenter.y + (wSize.y / 2 -  wSize.y * yNoiseValue);

		//Get Z value
		p.noiseSeed(zSeed);
		float zNoiseValue = p.noise(zOff);
		float zPosition = wCenter.z + (wSize.z / 2 -  wSize.z * zNoiseValue);
		
		//Set position
		position.set(xPosition, yPosition, zPosition);
		
		//update noise offsets
		xOff += NOISESCALE;
		yOff += NOISESCALE;
		zOff += NOISESCALE;

		System.out.println("POSITION: " + position.toString());
		System.out.println();
	}

	public PVector getPosition() {
		return position;
	}

}
